using IcsSEMCtrl.Global;
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Globalization;
using System.IO;
namespace IcsSEMCtrl.Util
{
	public class LocalLog
	{
		private static string DATE_FORMAT_LOGDATA = "yyyy-MMM-dd HH:mm:ss:fff";
		private static string DATE_FORMAT_FILE = "yyyy-MMM-dd-HHmmssfff";
		private bool localOutput = false;
		private static LocalLogInfo localLogInfo = new LocalLogInfo(true, 5, 10000);
		private FileStream fileStream = null;
		private StreamWriter streamWriter = null;
		public static LocalLog instance = new LocalLog();
		private string program = string.Empty;
		private string lastCreatedFileName = string.Empty;
		private long currentFileSize = 0L;
		private long maxFileSize = 0L;
		private string logDirName = string.Empty;
		private string suffix = ".log";
		private int suffixCount = 0;
		private LocalLog()
		{
		}
		public void SetLocalLogInfo(LocalLogInfo localLogInfo)
		{
			this.maxFileSize = (long)Math.Max(1, localLogInfo.entries);
		}
		public LocalLogInfo GetLocalLogInfo()
		{
			return LocalLog.localLogInfo;
		}
		public static LocalLog GetInstance()
		{
			return LocalLog.instance;
		}
		public void Init(string prog)
		{
			if (this.streamWriter == null)
			{
				this.program = prog;
				this.CreateNewFile(this.program);
			}
		}
		public void SetLocalOutput(bool value)
		{
			this.localOutput = value;
		}
		public void EnableLog(bool value)
		{
			LocalLog.localLogInfo.enabled = value;
		}
		public void Log(string data)
		{
			this.Log(DateTime.Now, data);
		}
		public void Log(DateTime date, string data)
		{
			if (LocalLog.localLogInfo.enabled)
			{
				string value = "(" + date.ToString(LocalLog.DATE_FORMAT_LOGDATA, DateTimeFormatInfo.InvariantInfo) + ") " + data;
				if (!this.localOutput)
				{
					if (this.streamWriter != null)
					{
						if (this.currentFileSize > this.maxFileSize - 1L)
						{
							this.CreateNewFile(this.program);
						}
						this.streamWriter.WriteLine(value);
						this.streamWriter.Flush();
						this.currentFileSize += 1L;
					}
				}
			}
		}
		private void CreateNewFile(string prog)
		{
			if (this.fileStream != null)
			{
				this.fileStream.Close();
			}
			if (this.logDirName.Equals(string.Empty))
			{
				NameValueCollection appSettings = ConfigurationManager.AppSettings;
				this.logDirName = appSettings[ECPProperties.ECP_BASE].ToString() + "\\logs";
			}
			if (!Directory.Exists(this.logDirName))
			{
				Directory.CreateDirectory(this.logDirName);
			}
			try
			{
				string text = string.Concat(new string[]
				{
					this.logDirName,
					"\\",
					prog,
					"-",
					DateTime.Now.ToString(LocalLog.DATE_FORMAT_FILE, DateTimeFormatInfo.InvariantInfo)
				});
				this.suffixCount = (this.lastCreatedFileName.Equals(text) ? (++this.suffixCount) : 0);
				string str = "-" + Convert.ToString(this.suffixCount) + this.suffix;
				this.fileStream = new FileStream(text + str, FileMode.Append, FileAccess.Write);
				this.streamWriter = new StreamWriter(this.fileStream);
				this.currentFileSize = 0L;
				this.lastCreatedFileName = text;
			}
			catch (IOException ex)
			{
				throw ex;
			}
		}
	}
}
